<?php
class xl_bannner{
    function read_list(){
        global $dbh;
        $sql='SELECT banner.*,location.name AS name_location,product.name AS name_product FROM banner,location,product WHERE banner.location_id=location.id AND banner.product_id=product.id ORDER BY id DESC';
        $sth=$dbh->prepare($sql);
        $sth->execute();
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
    function read_product(){
        global $dbh;
        $sql='SELECT id,name,category_id,brand_products_id FROM product';
        $sth=$dbh->prepare($sql);
        $sth->execute();
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
    function read_location(){
        global $dbh;
        $sql='SELECT id,name,parent FROM location';
        $sth=$dbh->prepare($sql);
        $sth->execute();
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
    function test_name($alias){
        global $dbh;
        $sql='SELECT alias FROM banner WHERE alias=:alias LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':alias'=>$alias));
        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }
    function insert_banner($data){
        global $dbh;
        $sql='INSERT INTO banner(name,title,keyword,description,product_id,alias,image,location_id,date_added,date_modified,status) VALUE(:name,:title,:keyword,:description,:product_id,:alias,:image,:location_id,:date_added,:date_modified,:status)';
        $sth=$dbh->prepare($sql);
        $sth->execute($data);
        return $sth;
    }
    function check_id($id){
        global $dbh;
        $sql='SELECT * FROM banner WHERE id=:id LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function check_name($id,$name){
        global $dbh;
        $sql='SELECT id,name FROM banner WHERE id!=:id AND name=:name LIMIT 0,1';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id,':name'=>$name));
        return $sth->fetch(PDO::FETCH_ASSOC);
    }
    function delete_banner($id){
        global $dbh;
        $sql='DELETE FROM banner WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
    }
    function update($data){
        global $dbh;
        $sql='UPDATE banner SET name=:name,title=:title,keyword=:keyword,description=:description,product_id=:product_id,location_id=:location_id,image=:image,status=:status,date_modified=:date_modified WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute($data);
        return $sth;
    }
    function update_status($id){
        global $dbh;
        $sql='UPDATE banner SET status=1-status WHERE id=:id';
        $sth=$dbh->prepare($sql);
        $sth->execute(array(':id'=>$id));
        return $sth;
    }
}